<%--
  Created by IntelliJ IDEA.
  User: 15972
  Date: 2022/6/29
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>FPA性格测试题目</title>

    <link rel="stylesheet" href="../../static/layui-main/dist/css/layui.css">
    <script src="../../static/layui-main/src/layui.js"></script>
</head>
<body>
<!-- 搜索框 -->
<form class="layui-form" action="" id="searchForm">
    <div class="layui-inline">
        <label class="layui-form-label">问题</label>
        <div class="layui-input-inline">
            <input type="text" id="topic" name="topic" placeholder="请输入查询的问题" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-inline">
            <input type="text" id="types" name="types" placeholder="请输入查询的类型" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" id="createBy" name="createBy" placeholder="请输入创建人" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo" id="reserch">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="search_reset">重置</button>
        </div>
    </div>
</form>

<!-- 头部工具栏 -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn  layui-icon layui-icon-add-circle" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-danger layui-icon layui-icon-delete" lay-event="delete">删除</button>
    </div>
</script>

<!-- 行内工具栏 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-dange layui-btn-xs layui-icon layui-icon-edit" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-icon layui-icon-delete" lay-event="del">删除</a>
</script>

<%--        添加问题弹出表单--%>
<form class="layui-form" action="" lay-filter="addQuestionFrom" id="addQuestionFrom" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">问题描述</label>
        <div class="layui-input-block">
            <input type="text" name="topic" required lay-verify="required" placeholder="请输入问题描述" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <textarea name="optionA" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <textarea name="optionB" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <textarea name="optionC" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <textarea name="optionD" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-block">
            <select name="types" lay-verify="required">
                <option value="">请选择问题类型</option>
                <option value="FPA前十五题">FPA前十五题</option>
                <option value="FPA后十五题">FPA后十五题</option>
                <option value="SAS焦虑测试">SAS焦虑测试</option>
                <option value="SDS抑郁测试">SDS抑郁测试</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="states" lay-skin="switch" lay-filter="switchUpdate" lay-text="可用|不可用">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addQuestionFrom">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<%--        修改问题弹出表单--%>
<form class="layui-form" action="" lay-filter="updateQuestionFrom" id="updateQuestionFrom" style="display: none">
    <%--            问题编号（隐藏）--%>
    <div class="layui-inline">
        <input type="text" id="topicId" name="topicId" autocomplete="off" class="layui-input" style="display: none">
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">问题描述</label>
        <div class="layui-input-block">
            <input type="text" name="topic" required lay-verify="required" placeholder="请输入问题描述" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项A</label>
        <div class="layui-input-block">
            <textarea name="optionA" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项B</label>
        <div class="layui-input-block">
            <textarea name="optionB" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项C</label>
        <div class="layui-input-block">
            <textarea name="optionC" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">选项D</label>
        <div class="layui-input-block">
            <textarea name="optionD" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">问题类型</label>
        <div class="layui-input-block">
            <select name="types" lay-verify="required">
                <option value="">请选择问题类型</option>
                <option value="FPA前十五题">FPA前十五题</option>
                <option value="FPA后十五题">FPA后十五题</option>
                <option value="SAS焦虑测试">SAS焦虑测试</option>
                <option value="SDS抑郁测试">SDS抑郁测试</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="states" lay-skin="switch" lay-filter="switchUpdate" lay-text="可用|不可用">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateQuestionFrom">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!-- table表格 -->
<%--lay-filter相当于dom元素的id，layui专用的选择器--%>
<table id="demo" lay-filter="test"></table>

</body>
<script>
    layui.use(['table', 'form', 'jquery', 'laydate'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;

        //第一个实例,渲染数据
        table.render({
            elem: '#demo'
            , height: 500
            , url: '${pageContext.request.contextPath}/fpa/findAllFpaQuestion.do'//数据接口
            , method: 'POST'
            , page: true//打开分页
            , limit: 10
            , limits: [5, 10, 15, 20, 25, 30, 50, 100]
            , autoSort: true//禁用前端的自动排序，开启后端的服务器排序
            , toolbar: '#toolbarDemo'
            , cols: [[
                //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'topicId', title: '编号', width: 80, sort: true, fixed: 'left'}
                , {field: 'topic', title: '问题', width: 100}
                , {field: 'optionA', title: '选项A', width: 100}
                , {field: 'optionB', title: '选项B', width: 100, sort: true}
                , {field: 'optionC', title: '选项C', width: 100}
                , {field: 'optionD', title: '选项D', width: 100}
                , {field: 'types', title: '类型', width: 120}
                , {
                    field: 'status', title: '状态', width: 70,
                    templet: function (d) {
                        //得到当前行数据，并拼接成自定义模板
                        if (d.states == '0') {
                            //return '可用'
                            return '<a class="layui-btn  layui-btn-xs">可用</a>'
                        }
                        if (d.states == '1') {
                            return '<a class="layui-btn layui-btn-disabled  layui-btn-xs">不可用</a>'
                        }

                    }
                }
                , {field: 'createTime', title: '创建时间', width: 110}
                , {field: 'createBy', title: '创建人', width: 90}
                , {field: 'updateTime', title: '修改时间', width: 110}
                , {field: 'updateBy', title: '修改人', width: 90}
                , {fixed: 'right', title: '操作', width: 150, align: 'center', toolbar: '#barDemo'}
            ]]
        });

        //触发排序事件
        table.on('sort(test)', function (obj) { //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值 "
            console.log(obj.field); //当前排序的字段名
            console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）
            console.log(this); //当前排序的 th 对象

            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
            table.reload('demo', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                , type: 'post'
                , where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    , order: obj.type //排序方式
                }
                , method: 'post'
            });
        });

        //模糊查询,监听提交
        form.on('submit(formDemo)', function (data) {
            table.reload('demo', {
                where: {
                    topic: data.field.topic
                    , types: data.field.types
                    , createBy: data.field.createBy
                }
                , page: {
                    curr: 1
                }
            });
            return false;
        });

        //重置按钮绑定单击事件
        $("#search_reset").click(function () {
            table.reload('demo', {
                where: {
                    topic: ""
                    , types: ""
                    , createBy: ""
                },
                page: {
                    curr: 1
                }
            });
        });

        //监听头工具栏事件(表头的添加与删除)
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '添加问题',
                        type: 1,
                        content: $('#addQuestionFrom'),
                        area: ['500px', '500px']
                    });
                    //清空输入的添加数据
                    form.val('addQuestionFrom', {
                        "topic": "",
                        "optionA": "",
                        "optionB": "",
                        "optionC": "",
                        "optionD": "",
                        "types": "",
                        "states": "",
                        "createTime": "",
                        "createBy": "",
                        "updateTime": "",
                        "updateB": ""
                    })
                    //监听addUserForm表单的提交事件toolbarDemo
                    form.on('submit(addQuestionFrom)', function (data) {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/fpa/addFpaQuestion.do"
                            , data: data.field
                            , dataType: "json"
                            , type: "post"
                            , success: function (redata) {
                                if (redata.code == 0) {
                                    layer.closeAll();//关闭对话框
                                    table.reload('demo', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                }
                                layer.msg(redata.msg);
                            }
                        });
                        return false;
                    });
                    break;
                case  'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //定义一个数组，封装所有选中的id
                        var ids = [];
                        //循环所有选中的行
                        for (let i = 0; i < data.length; i++) {
                            ids[i] = data[i].topicId;
                        }
                        console.log(data)
                        layer.confirm('真的删除选中行吗？', function (index) {
                            $.ajax({
                                url: '${pageContext.request.contextPath}/fpa/deleteFpaQuestion.do'
                                , data: {topicId: ids.join(",")}
                                , dataType: "JSON"
                                , type: "POST"
                                , success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();
                                        //刷新表格数据
                                        table.reload('demo', {
                                            page: {
                                                curr: 1 //重新从第一页开始
                                            }
                                        });
                                    }
                                    layer.msg(data.msg)
                                }
                            });
                        });
                    }
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            // 回显状态开关
            if (data.states.valueOf() == "1") {
                $("#updateQuestionFrom input[type=checkbox]").prop("checked", "");
            } else {
                $("#updateQuestionFrom input[type=checkbox]").prop("checked", "checked");
            }

            if (layEvent === 'update') {
                layer.open({
                    title: '修改问题'
                    , type: 1
                    , content: $('#updateQuestionFrom')
                    , area: ["500px", "500px"]
                });

                //回显用户信息
                form.val('updateQuestionFrom', {
                    "topicId": data.topicId,
                    "topic": data.topic,
                    "optionA": data.optionA,
                    "optionB": data.optionB,
                    "optionC": data.optionC,
                    "optionD": data.optionD,
                    "types": data.types,
                    "createTime": data.createTime,
                    "createBy": data.createBy,
                })


                //日期
                laydate.render({
                    elem: '#date1'
                });
                laydate.render({
                    elem: '#date2'
                });
                // 更新用户信息
                form.on('submit(updateQuestionFrom)', function (data) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/fpa/updateFpaQuestion.do"
                        , data: data.field
                        , dataType: "json"
                        , type: "post"
                        , success: function (redata) {
                            if (redata.code == 0) {
                                layer.closeAll();//关闭对话框
                                layer.msg(redata.msg);
                                table.reload('demo', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                // 改变状态开关
                                if (data.states.valueOf() == "1") {
                                    $("#updateQuestionFrom input[type=checkbox]").prop("checked", "");
                                } else {
                                    $("#updateQuestionFrom input[type=checkbox]").prop("checked", "checked");
                                }

                            } else {
                                layer.msg(redata.msg);
                            }
                        }
                    });
                    return false;
                });
            } else if (layEvent === 'del') {
                layer.confirm('真的删除吗？', function (index) {
                    console.log(index)
                    $.ajax({
                        url: "${pageContext.request.contextPath}/fpa/deleteFpaQuestion.do"
                        , dataType: "JSON"
                        , type: "POST"
                        , data: {topicId: data.topicId}
                        , success: function (data) {
                            if (data.code == 0) {
                                layer.closeAll(); //疯狂模式，关闭所有层
                                table.reload('demo', {
                                    page: {
                                        curr: 1 //重新从第一页开始
                                    }
                                });
                            }
                            layer.msg(data.msg);
                        }
                    });
                })
            }


        });

    });
</script>
</body>
</html>
